{
  "name": "Logistics Shipment Tracking & Delivery Issues Agent",
  "nodes": [
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "id": "747b72ab-72ac-4b4b-9453-c4738b65c4f4",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        0
      ],
      "webhookId": "e69fd63a-4cf6-443e-ac3e-4d7bcb04f5d2",
      "credentials": {
        "telegramApi": {
          "id": "NFpPTqiHG1arwJ14",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "id-1",
              "name": "customer_message",
              "value": "={{ $json.message.text }}",
              "type": "string"
            },
            {
              "id": "id-2",
              "name": "customer_name",
              "value": "={{ $json.message.from.first_name + ' ' + ($json.message.from.last_name || '') }}",
              "type": "string"
            },
            {
              "id": "id-3",
              "name": "chat_id",
              "value": "={{ $json.message.chat.id }}",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "id": "b2faa358-d82f-487c-996d-d5f0205d8afd",
      "name": "Workflow Configuration",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        224,
        0
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=Customer Name: {{ $json.customer_name }}\nCustomer Message: {{ $json.customer_message }}\nChat ID: {{ $json.chat_id }}",
        "options": {
          "systemMessage": "=You are a Logistics Shipment Tracking and Delivery Issues Assistant.\n\nYour responsibilities:\n1. Identify whether the customer wants to REPORT a delivery issue or CHECK shipment tracking status.\n2. For new delivery issues, collect:\n   - Tracking Number\n   - Date of Issue\n   - Issue Type (e.g., Delayed Delivery, Damaged Package, Missing Item, Wrong Address, etc.)\n   - Brief description\n3. Validate the shipment using the Shipment Master Database Tool and Extract the Customer_Name.\n4. If the shipment is valid:\n   - Generate a unique issue reference ID in format: ISU-YYYYMMDD-[last 4 digits of tracking]-HHmmss\n   - Register the issue using Delivery Issue Register Tool\n   - Send email confirmation using Gmail Tool\n   - Clearly explain resolution process and timelines\n   - Respond to the customer using Telegram Response Tool\n5. If the shipment is invalid or not found:\n   - Politely explain the reason\n   - Do not register issue\n   - Respond using Telegram Response Tool\n6. For tracking status checks:\n   - Ask for tracking number if missing\n   - Retrieve status using Shipment Tracking Tool\n   - Explain the current location and stage in simple language\n   - Provide expected delivery date and time\n   - Respond using Telegram Response Tool\n\nStrict rules:\n- Do NOT make delivery commitments outside operational capacity.\n- Do NOT guarantee compensation without verification.\n- Be clear, calm, and professional.\n- Maintain a helpful, empathetic tone - delivery issues cause inconvenience.\n- Avoid logistics jargon.\n- Ask follow-up questions if information is missing.\n- ALWAYS use the Telegram Response Tool to send your final response to the customer."
        }
      },
      "id": "20d0be18-bcfc-450e-ba5e-ee00f5b44b5f",
      "name": "Logistics Tracking Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3,
      "position": [
        496,
        0
      ]
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "id",
          "value": "gpt-4o"
        },
        "builtInTools": {},
        "options": {
          "temperature": 0.2
        }
      },
      "id": "7f788952-6b47-4dbc-91be-0b7e9cfe1a3a",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [
        320,
        224
      ],
      "credentials": {
        "openAiApi": {
          "id": "ICwxUBbatsF2sDvy",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Workflow Configuration').item.json.chat_id }}",
        "contextWindowLength": 10
      },
      "id": "82962153-520f-4a30-9df1-909b4f6505fe",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        480,
        224
      ]
    },
    {
      "parameters": {
        "descriptionType": "manual",
        "toolDescription": "Use this tool to validate shipment information. Provide the tracking number to check if the shipment exists, current status, and delivery details. Returns shipment_status, origin, destination, and estimated_delivery_date.",
        "documentId": {
          "__rl": true,
          "value": "1CRsugg1k6BShT6m1EwJlJCE7FmR5FezYdAc3eps8wH0",
          "mode": "list",
          "cachedResultName": "Shipment_Master_Database",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1CRsugg1k6BShT6m1EwJlJCE7FmR5FezYdAc3eps8wH0/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1CRsugg1k6BShT6m1EwJlJCE7FmR5FezYdAc3eps8wH0/edit#gid=0"
        },
        "options": {}
      },
      "id": "9ccfaa85-e36f-420c-958f-c3a533d5eb75",
      "name": "Shipment Master Database Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [
        640,
        224
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "LOs2dbk9lby0NfDM",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "descriptionType": "manual",
        "toolDescription": "=Use this tool to register a new delivery issue. Provide issue_reference_id, tracking_number, issue_date, issue_type, description, and issue_status (set to Reported). This creates an official delivery issue record.",
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "1gzuioH4Gg77ECQfsQBGk0CFeoedpc5p1PgrQCpI7SDI",
          "mode": "list",
          "cachedResultName": "Delivery_Issue_Register",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1gzuioH4Gg77ECQfsQBGk0CFeoedpc5p1PgrQCpI7SDI/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1gzuioH4Gg77ECQfsQBGk0CFeoedpc5p1PgrQCpI7SDI/edit#gid=0"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Issue_Ref_ID": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Issue_Ref_ID', ``, 'string') }}",
            "Tracking_Number": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Tracking_Number', ``, 'string') }}",
            "Customer_Name": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Customer_Name', ``, 'string') }}",
            "Issue_Date": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Issue_Date', ``, 'string') }}",
            "Issue_Type": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Issue_Type', ``, 'string') }}",
            "Description": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Description', ``, 'string') }}",
            "Issue_Status": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Issue_Status', ``, 'string') }}",
            "Created_At": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Created_At', ``, 'string') }}"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "Issue_Ref_ID",
              "displayName": "Issue_Ref_ID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Tracking_Number",
              "displayName": "Tracking_Number",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Customer_Name",
              "displayName": "Customer_Name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Issue_Date",
              "displayName": "Issue_Date",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Issue_Type",
              "displayName": "Issue_Type",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Description",
              "displayName": "Description",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Issue_Status",
              "displayName": "Issue_Status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Created_At",
              "displayName": "Created_At",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "id": "f1b8eced-5df9-4f24-8278-a3ab354a8205",
      "name": "Delivery Issue Register Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [
        784,
        224
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "LOs2dbk9lby0NfDM",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "descriptionType": "manual",
        "toolDescription": "Use this tool to check real-time tracking status of a shipment. Provide the tracking_number to retrieve current location, transit history, expected delivery date, and any delays or issues.",
        "documentId": {
          "__rl": true,
          "value": "1ZC9iW1IqoC5OlCvUX08tI6uU8T3R-n1yz7C4Emcu_kI",
          "mode": "list",
          "cachedResultName": "Shipment_Tracking",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1ZC9iW1IqoC5OlCvUX08tI6uU8T3R-n1yz7C4Emcu_kI/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1ZC9iW1IqoC5OlCvUX08tI6uU8T3R-n1yz7C4Emcu_kI/edit#gid=0"
        },
        "options": {}
      },
      "id": "62745757-a7e6-4002-b850-fa6d36cf06be",
      "name": "Shipment Tracking Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [
        928,
        224
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "LOs2dbk9lby0NfDM",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "descriptionType": "manual",
        "toolDescription": "Use this tool to send an official delivery issue acknowledgement email to the customer. Provide recipient email, issue reference ID, tracking number, issue type, and resolution timeline.",
        "sendTo": "={{ $fromAI('customer_email', 'Customer email address', 'string') }}",
        "subject": "={{ $fromAI('email_subject', 'Email subject line', 'string', 'Delivery Issue Acknowledgement') }}",
        "message": "={{ $fromAI('email_body', 'Email body content with issue details', 'string') }}",
        "options": {}
      },
      "id": "ba1015d8-26e0-481e-ac5f-e62fb4130470",
      "name": "Gmail Tool",
      "type": "n8n-nodes-base.gmailTool",
      "typeVersion": 2.1,
      "position": [
        1072,
        224
      ],
      "webhookId": "15cba1c5-81ac-4d4d-9a13-35fc51ad3bbe",
      "credentials": {
        "gmailOAuth2": {
          "id": "cyqCGWcggZNMcSOv",
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "={{ $json.output }}",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        928,
        0
      ],
      "id": "d74dc4e0-f783-4643-92e8-242ca666f514",
      "name": "Send a text message",
      "webhookId": "3c111795-fe7d-497d-b853-a6432330a256",
      "credentials": {
        "telegramApi": {
          "id": "NFpPTqiHG1arwJ14",
          "name": "Telegram account"
        }
      }
    }
  ],
  "pinData": {},
  "connections": {
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Workflow Configuration",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Workflow Configuration": {
      "main": [
        [
          {
            "node": "Logistics Tracking Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Logistics Tracking Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "Logistics Tracking Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Shipment Master Database Tool": {
      "ai_tool": [
        [
          {
            "node": "Logistics Tracking Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Delivery Issue Register Tool": {
      "ai_tool": [
        [
          {
            "node": "Logistics Tracking Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Shipment Tracking Tool": {
      "ai_tool": [
        [
          {
            "node": "Logistics Tracking Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Tool": {
      "ai_tool": [
        [
          {
            "node": "Logistics Tracking Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Logistics Tracking Agent": {
      "main": [
        [
          {
            "node": "Send a text message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "489157c1-ee94-4606-9645-459113c4f56d",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "3a43da28588548e21903e71cf1dc3ddd65c24bf0c62e7e4b77542ffe87ad79c6"
  },
  "id": "R9QkaK1UMXwvuYjF",
  "tags": []
}